package com.itheima.controller;

import com.itheima.common.Code;
import com.itheima.common.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.vo.PagingQueryVo;
import com.itheima.vo.SetmealVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author :WuJunJie
 * packageName :com.itheima.controller
 * fileName :SetMealController
 * creationTime :2022-11-24 20:10
 */
@RestController
@RequestMapping("setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @PostMapping
    public Result insertSetMeal(@RequestBody SetmealVo setmealVo){

        Boolean insert=setmealService.insertSetMeal(setmealVo);
        if (insert){
            return Result.success();
        }
        return Result.error(Code.Forbidden.getCode());
    }

    @PutMapping
    public Result updateSetmeal(@RequestBody SetmealVo setmealVo){
        Boolean update=setmealService.updateSetmeal(setmealVo);
        if(update){
            return Result.success();
        }
        return Result.error(Code.Forbidden.getCode());
    }


    @DeleteMapping
    public Result deleteSetmeal(@RequestParam(name = "ids")List<String> ids){
            Integer delete=setmealService.deleteSetmeal(ids);
            if (delete==1){
                return Result.success();
            }
            if (delete==-1){
                return Result.error("你要删除的ID中有正在起售的商品，不能删除！");
            }
        return Result.error(Code.Forbidden.getCode());
    }

    @PostMapping("status/{status}")
    public Result changeStatus(@PathVariable(name = "status")Integer status,@RequestParam(name="ids")List<String> ids){
        Boolean change=setmealService.changeStatus(status,ids);
        if(change){
            return Result.success();
        }
        return Result.error(Code.Forbidden.getCode());
    }


    @GetMapping("page")
    public Result selectPage(@RequestParam(name = "name",required = false) String name,
            @RequestParam(name = "page",defaultValue = "1") Integer page,
            @RequestParam(name="pageSize",defaultValue = "2")Integer pageSize){
        PagingQueryVo<Setmeal> setmealVo=setmealService.selectPage(name,page,pageSize);
        return Result.success(setmealVo);
    }
    @GetMapping("/{id}")
    public Result selectById(@PathVariable(name = "id") String id){
        SetmealVo setmealVo= setmealService.selectById(id);
        return Result.success(setmealVo);
    }
}
